﻿using MicroAmoy.Application.Code;
using MicroAmoy.Application.Entity.CustomerManage;
using MicroAmoy.Application.IService.CustomerManage;
using MicroAmoy.Application.IService.SystemManage;
using MicroAmoy.Application.Service.SystemManage;
using MicroAmoy.Data.Repository;
using MicroAmoy.Util.Extension;
using MicroAmoy.Util.WebControl;
using MicroAmoy.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Common;
namespace MicroAmoy.Application.Service.CustomerManage
{
    /// <summary>
    /// 版 本 6.1
    /// Copyright (c) 2013-2016 云云科技工作室
    /// 创 建：超级管理员
    /// 日 期：2017-03-28 15:21
    /// 描 述：产品类
    /// </summary>
    public class MAProductService : RepositoryFactory<MAProductEntity>, IMAProductIService
    {
        #region 获取数据
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns>返回列表</returns>
        public IEnumerable<MAProductEntity> GetList(string queryJson)
        {
            var expression = LinqExtensions.True<MAProductEntity>();
            var queryParam = queryJson.ToJObject();
            if (!queryParam["CustomerId"].IsEmpty())
            {
                string cid = queryParam["CustomerId"].ToString();
                expression = expression.And(t => t.CustomerId.Contains(cid));
            }
            return this.BaseRepository().IQueryable(expression).ToList(); 
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        public MAProductEntity GetEntity(string keyValue)
        {
            return this.BaseRepository().FindEntity(keyValue);
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        public void RemoveForm(string keyValue)
        {
            this.BaseRepository().Delete(keyValue);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="keyValue">主键组</param>
        public void RemoveForm(object[] keyValue)
        {
            this.BaseRepository().Delete(keyValue);
        }
        /// <summary>
        /// 保存表单（新增、修改）
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public void SaveForm(string keyValue, MAProductEntity entity)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                this.BaseRepository().Update(entity);
            }
            else
            {
                entity.Create();
                this.BaseRepository().Insert(entity);
            }
        }
        #endregion

        public IEnumerable<MAProductEntity> GetPageList(Pagination pagination, string queryJson)
        {
            var expression = LinqExtensions.True<MAProductEntity>();
            var queryParam = queryJson.ToJObject();
            //查询条件
            //产品编号
            if (!queryParam["ProductId"].IsEmpty())
            {
                string pid = queryParam["ProductId"].ToString();
                expression = expression.And(t => t.ProductId.Contains(pid));
            }
            //产品标题
            if (!queryParam["ProductTitle"].IsEmpty())
            {
                string ProductTitle = queryParam["ProductTitle"].ToString();
                expression = expression.And(t => t.ProductTitle.Contains(ProductTitle));
            }
            //类型
            if (!queryParam["Type"].IsEmpty())
            {
                int type = queryParam["Type"].ToInt();
                expression = expression.And(t => t.Type == type);
            }
            //客户
            if (!queryParam["CustomerId"].IsEmpty())
            {
                string cid = queryParam["CustomerId"].ToString();
                expression = expression.And(t => t.CustomerId.Contains(cid));
            }
            return this.BaseRepository().FindList(expression, pagination);
        }
    }
}
